package com.quan.base.common.utils.file;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.mzlion.core.io.FilenameUtils;

public class FileUpload {

	/**
	 * 文件上传 <br>
	 * 上传文件路径：临时目录 <br>
	 * 文件名称：随机名称 <br>
	 * @param request
	 * @return
	 */
	public static FileVo upload(HttpServletRequest request) {
		return upload(request, null, null) ;
	}
	
	/**
	 * 文件上传
	 * 文件保存路径：临时目录 <br>
	 * @param request
	 * @param destDir 指定保存的目录名称(/weixin)
	 * @return
	 */
	public static FileVo upload(HttpServletRequest request, String destDir) {
		return upload(request, null, destDir) ;
	}
	
	/**
	 * 文件上传
	 * @param request
	 * @param realPath 指定文件保存的绝对路径
	 * @param destDir 指定保存的目录名称
	 * @return
	 */
	public static FileVo upload(HttpServletRequest request, String realPath, String destDir) {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

		String saveFilePath = null ;
		
		MultiValueMap<String, MultipartFile> multiFileMap = multipartRequest.getMultiFileMap();
		Set<String> keySet = multiFileMap.keySet();
		for (String key : keySet) {
			List<MultipartFile> list = multiFileMap.get(key);
			for (MultipartFile multipartFile : list) {
				try {
					String fileName = null ;
					String originalFilename = multipartFile.getOriginalFilename() ;
					String fileExt = FilenameUtils.getFileExt(originalFilename) ;
					long size = multipartFile.getSize() ;
					String fileSize = FileSizeHelper.getHumanReadableFileSize(size) ;
					String contentType = com.quan.base.common.utils.file.FileUtils.getContentType(originalFilename) ;
 					
					if(StringUtils.isBlank(realPath)) {
						realPath = FileUtils.getTempDirectoryPath() ;
					}
					if(StringUtils.isNotBlank(destDir)) {
						saveFilePath = realPath + UploadUtils.generateFilename(destDir, fileExt) ;
					} else {
						saveFilePath = realPath + UploadUtils.generateRamdonFilename(fileExt) ;
					}
					
					fileName = FilenameUtils.getFilename(saveFilePath) ;
					
					//上传文件到指定目录
					FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), new File(saveFilePath));
					
					return new FileVo(realPath, destDir, fileName, fileExt, contentType, size, fileSize, saveFilePath) ;
					
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}
	
	public static void main(String[] args) {
		System.out.println(FileUtils.getUserDirectory());
		System.out.println(FileUtils.getUserDirectoryPath());
		System.out.println(FileUtils.getTempDirectoryPath());
		System.out.println(FileUtils.getTempDirectoryPath());
		System.out.println(FileUtils.getTempDirectory());
		
		System.out.println(FilenameUtils.getFilename("D://mars//tools//server//quan//6015-tomcat-8.5.23//temp/201803/10174343rpx8.png"));
	}
	
}
